#include "stdafx.h"
#include <Shlobj.h>
#include <Shlwapi.h>
#include <Strsafe.h>
#include "Exec.h"
#include "Registry.h"
#include "Setup.h"

TCHAR Setup::autoRun[]=L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
TCHAR Setup::modPath[MAX_PATH+1];
TCHAR Setup::modName[MAX_PATH+1];
TCHAR Setup::regPath[MAX_PATH+1];

uint Setup::check(PTSTR appName){
	try{
		CurrentUser reg(autoRun);
		String* t=reg.get(appName,(TCHAR*)0);
		TCHAR m[MAX_PATH+1];
		GetModuleFileName(0,m,MAX_PATH);
		ubyte res=!StrCmp(m,t->Ptstr());
		delete t;
		return res;
	}catch(int){
	}
	return 0;
}
void Setup::copy(PTSTR appName){
	CurrentUser reg(autoRun);
	setPaths();
	if(StrCmp(modPath,regPath)){
		CreateDirectory(regPath,NULL);
		StringCchCat(regPath,MAX_PATH,modName);
		CopyFile(modPath,regPath,0);
	}else StringCchCopy(regPath,MAX_PATH,modPath);
	reg.set(appName,regPath);
}
void Setup::exec(PTSTR noupgrade){
	Exec::exec(regPath,noupgrade);
}
void Setup::set(PTSTR appName,uint run){
	CurrentUser reg(autoRun);
	if(run){
		TCHAR m[MAX_PATH+1];
		GetModuleFileName(0,m,MAX_PATH);
		reg.set(appName,m);
	}else reg.del(appName);
}
void Setup::setPaths(void){
	GetModuleFileName(0,modPath,MAX_PATH);
	if(SUCCEEDED(SHGetFolderPath(NULL,CSIDL_PROGRAM_FILES,NULL,0,regPath))){
		StringCchCat(regPath,MAX_PATH,_T("\\Phoxell\\"));
		StringCchCopy(modName,MAX_PATH,_T("xync.exe"));
	}else StringCchCopy(regPath,MAX_PATH,modPath);
}
